// 电影相关接口
const cloud = require('wx-server-sdk')
var rq = require('request-promise')

cloud.init()
// 一、电影详情接口
// https://api.douban.com/v2/movie/subject/1292052?apikey=0df993c66c0c636e29ecbb5344252a4a

// 二、查询正在热映的电影
// https://api.douban.com/v2/movie/in_theaters?apikey=0df993c66c0c636e29ecbb5344252a4a&city=广州

// 三、获取电影Top250
// https://api.douban.com/v2/movie/top250?apikey=0df993c66c0c636e29ecbb5344252a4a&start=0&count=10

// 云函数入口函数
exports.main = async (event, context) => {
  var url = null;
  const log = cloud.logger()
  switch (event.action) {
    case 'getMovieDetail': {
      url = 'https://zhaoyj.work/poetry/movie/detail?movieId=' + event.movie_id;
    }
    break;
  case 'getMovieShowing': {
    url = "https://zhaoyj.work/poetry/movie/showing";
  }
  break;
  case 'getMovieTop250': {
    url = 'https://zhaoyj.work/poetry/movie/top250?limit=10&offset=' + event.start;
  }
  break
  default:
    return "请传入action值。包括：getMovieDetail、getMovieShowing、getMovieTop250"
  }
  var res = rq(url).then(html => {
    return JSON.parse(html);
  }).catch(err => {
    console.log(err);
  })
  return res
}